﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="hanldleAdd()">新建</a-button>
      <a-button
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
        >删除</a-button
      >
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="Title">标题</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="() => ((queryParam = {}), this.getDataList())">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record.Id)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'

const columns = [
  { title: '标题', dataIndex: 'Title', width: '30%' },
  { title: '审批级数', dataIndex: 'Level', width: '30%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } },
]

export default {
  components: {
    EditForm,
  },
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      disabled: false,
      data: [],
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      queryParam: {},
      selectedRowKeys: [],
      loading: false,
      columns,
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      // this.getDataList()
    },
    getDataList() {
      // console.log(123)
      // this.selectedRowKeys = []
      // this.loading = true
      // this.$http
      //   .post('/FP/Base_OverFlow/GetDataList', {
      //     PageIndex: this.pagination.current,
      //     PageRows: this.pagination.pageSize,
      //     SortField: this.sorter.field || 'Id',
      //     SortType: this.sorter.order,
      //     Search: this.queryParam,
      //     ...this.filters
      //   })
      //   .then(resJson => {
      //     this.loading = false
      //     this.data = resJson.Data
      //     const pagination = { ...this.pagination }
      //     pagination.total = resJson.Total
      //     this.pagination = pagination
      //   })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      this.disabled = false
      this.$refs.editForm.openForm('新增')
    },
    handleEdit(id) {
      this.disabled = true
      this.$refs.editForm.openForm('编辑', id)
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return (
          new Promise((resolve, reject) => {
            thisObj.$http.post('/FP/Base_OverFlow/DeleteData', ids).then((resJson) => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                // thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          }),
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        )
      })
    },
  },
}
</script>
